package com.lagou.edu.pojo;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 定义⼀个类实现了BeanPostProcessor，默认是会对整个Spring容器中所有的bean进⾏处理
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
     * Bean的初始化方法前执行
     * @param bean      bean的实例
     * @param beanName  bean的id
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if ("resultBean".equalsIgnoreCase(beanName)) {
            System.out.println("第6步 BeanPostProcessor before方法拦截处理: " + beanName);
        }
        return bean;
    }

    /**
     * Bean的初始化方法后执行
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("resultBean".equalsIgnoreCase(beanName)) {
            System.out.println("第9步 BeanPostProcessor after方法拦截处理: " + beanName);
        }
        return bean;
    }
}
